package com.hyt.it.ogt.controller.ykcj.grade;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.exception.exception.BusinessLogicException;
import com.hyt.it.ogt.common.config.ApiVersionConstant;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.annotation.Log;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.BusinessType;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.common.enums.ExamResultDownLoadFileTypeEnum;
import com.hyt.it.ogt.ykcj.domain.vo.EnterpriseExamineeInfoVo;
import com.hyt.it.ogt.ykcj.domain.vo.QueryEnterpriseExamineeGradeVo;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseGroupExamineeInfoService;
import com.hyt.loginfo.model.User;
import com.hyt.loginfo.service.IBaseLoginService;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.swagger.ApiVersion;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 考试组考生成绩信息Controller
 *
 * @author liying
 * @date 2021-11-19
 */
@Api(tags = "grade.20.考试组考生成绩信息")
@ApiSort(value = 213)
@RestController
@RequestMapping("/otherenterprise/group/grade/info")
@Slf4j
public class OtherEnterpriseGroupExamineeInfoController extends BaseController {

    @Autowired
    private IEnterpriseGroupExamineeInfoService enterpriseGroupExamineeInfoService;

    @Autowired
    private IBaseLoginService baseLoginService;


    /**
     * 查询考试组考生信息列表
     */
    @ApiOperation(value = "其它考试查询考试组考生信息列表", response = EnterpriseExamineeInfoVo.class)
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody QueryEnterpriseExamineeGradeVo queryExamineeGradeVo) {
        queryExamineeGradeVo.setDeptIds(baseLoginService.getDeptIds());
        PageInfo<List<EnterpriseExamineeInfoVo>> pageInfo = enterpriseGroupExamineeInfoService.selectEnterpriseExamineeGradePage(queryExamineeGradeVo);
        return getDataTable(pageInfo);
    }


    /**
     * 导出未归档其它考试总成绩信息列表
     */
    @Log(title = "导出未归档其它考试总成绩信息", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "2.1导出未归档其它考试总成绩信息列表", notes = "(参数为：examineeIds（非必须传），examId（必传）)" +
            "导出接口是获取文件名称，通过返回的文件名称再进行文件的导出,调用接口：/common/download，delete参数传true删除下载,  "
            + "200：成功；52292101：查询不到当前用户信息,52292102: 考试id不能为空,52292103: 文件正在下载中请耐心等待,52292104:下载的内容为空，下载失败")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examineeIds", value = "考生ids,不传则导出该考试下的所有学生", dataType = DataType.ARRAY,
                    paramType = ParamType.QUERY, required = false),
            @ApiImplicitParam(name = "examId", value = "考试id", dataType = DataType.STRING,
                    paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(3)
    @ApiVersion(group = {ApiVersionConstant.V2_4_10})
    @ProgressAnno(errorCode = 52293210, timeoutSeconds = 60 * 10)
    @PostMapping("/export")
    public void export(@RequestBody QueryEnterpriseExamineeGradeVo queryEnterpriseExamineeGradeVo) throws Exception {
        logger.info("# 导出其他考试考试组考生考试总成绩信息参数 ： {}", JSON.toJSON(queryEnterpriseExamineeGradeVo));
        if (StringUtils.isEmpty(queryEnterpriseExamineeGradeVo.getExamId())) {
            BusinessLogicException.throwException(52293102, "请选择一个考试");
        }
        User user = baseLoginService.getSysUser();
        if (null == user) {
            BusinessLogicException.throwException(52292101, "查询不到当前用户信息");
        }
        queryEnterpriseExamineeGradeVo.setDeptIds(baseLoginService.getDeptIds());
        queryEnterpriseExamineeGradeVo.setArchivedStatus("N");
        //方法里面有重新设置名称的，这里只是为了防止为空的设置，没太多意义
        queryEnterpriseExamineeGradeVo.setFileName(CommonEnums.EXPORT_FILE_NAME.getDesc());
        queryEnterpriseExamineeGradeVo.setSheetName(CommonEnums.EXPORT_SHEET_NAME.getDesc());
        ExamResultDownLoadFileTypeEnum fileTypeEnum = ExamResultDownLoadFileTypeEnum.OTHERUNARCHIVED;
        if (CommonEnums.ARCHIVED_STATUS_Y.getCode().equals(queryEnterpriseExamineeGradeVo.getArchivedStatus())) {
            fileTypeEnum = ExamResultDownLoadFileTypeEnum.OTHERARCHIVED;
        }
        enterpriseGroupExamineeInfoService.exportExcelByCondition(queryEnterpriseExamineeGradeVo, user, fileTypeEnum);
    }


}
